#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from flask import Response
from flask_restful import Resource
from src.routers import GetDataFromManager

sys.path.append("../..")


class MapboxTileServer(Resource):
    def get(self,servicename,level,column,row,ext):
        # level=int(level)+int(db.zoomoffset)
        level = int(level) + int(-1)

        resp = ''
        try:
            tile = GetDataFromManager.GetTileFromSLRC(servicename, level, row, column)
            resp = Response(tile['imgData'], status=200, mimetype='image/png')
        except Exception as e:
            print(e)

            resp = Response('MapServer Not Found' +
                            '\n\nlevel:' + str(level) +
                            '\nrow:' + str(row) +
                            '\ncolumn:' + str(column),
                            status=404,
                            mimetype='text/javascript')
        finally:
            return resp
